<?php
/**
 * Created by PhpStorm.
 * User: qiaoli
 * Date: 15/9/29
 * Time: 上午11:45
 */
class Hatem extends CI_Model
{
    // 标签下 缓存key前缀
    private $tag_heat_post_ids = 'tag:heat_post_ids_';
    // 科室下 缓存key前缀
    private $thread_heat_post_ids = 'thread:heat_post_ids_';
    // 全部下 缓存key前缀
    private $thread_tag_heat_post_ids = 'thread:tag:heat_post_ids';

    function __construct()
    {
        parent::__construct();
    }
    public function insert_hate_post($post_id,$user_id)
    {
        $info = array();
        $info['user_id'] = $user_id;
        $info['post_id'] = $post_id;
        $this->db->set($info);
        $result = $this->db->insert('ml_hatepost');
        if($result)
        {
            $return = $this->db->insert_id();
            //删除缓存
            $mc = $this->cache->memcached;
            if(!$this->cache->memcached->is_supported())
            {
                $result = array("code" => -3,'tips' => "缓存未开启");
            }
            $key = 'hate_postid_list_'.$user_id;
            $mc->delete($key);
            #查询帖子的所有标签和科室
            $tag_id   =  $this->db->select('type_id')->from('ml_posttype')->where(array('type'=>2,'post_id'=>$post_id))->get()->result_array();
            if($tag_id)
            {
                foreach ($tag_id as $tag)
                {
                    $mc->delete($this->tag_heat_post_ids . $tag['type_id']);
                }
            }
            $thread_id = $this->db->select('type_id')->from('ml_posttype')->where(array('type'=>1,'post_id'=>$post_id))->get()->result_array();
            if($thread_id)
            {
                foreach ($thread_id as $thread)
                {
                    $mc->delete($this->thread_heat_post_ids . $thread['type_id']);
                }
            }
            $mc->delete($this->thread_tag_heat_post_ids);

            $this->load->model('rssm');
            $this->rssm->delete_cache_page_data($user_id);
        }
        else
        {
            $return = false;
        }
        return $return;
    }
    public function hate_postid_list($user_id)
    {
        $mc = $this->cache->memcached;
        if(!$this->cache->memcached->is_supported())
        {
            $result = array("code" => -3,'tips' => "缓存未开启");
        }
        $key = 'hate_postid_list_'.$user_id;
        $post_ids = $mc->get($key);
        if(!$post_ids)
        {
            $post_ids =array();
            $result = $this->db->select("post_id")->where("user_id",$user_id)->get("ml_hatepost")->result_array();
            foreach($result as $row)
            {
                $post_ids[] = $row['post_id'];
            }
            if(!empty($post_ids))
            {
                $mc->save($key,$post_ids, 86400);
            }
        }
        return $post_ids;
    }
    public function is_have_exist($post_id,$user_id)
    {
        $post_ids = $this->hate_postid_list($user_id);
        if(in_array($post_id,$post_ids))
        {
            return true;
        }
        else
        {
            $count = $this->db->select("post_id")->where("user_id",$user_id)->where("post_id",$post_id)->from("ml_hatepost")->count_all_results();
            if($count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    //插入屏蔽
    public  function insert_hate($user_id,$user_name,$hate_id,$hate_name,$addtime)
    {
        $query = array('user_id'=>$user_id,'user_name'=>$user_name, 'hate_id'=>$hate_id, 'hate_name'=>$hate_name, 'addtime'=>$addtime);
        return $result = $this->db->insert("ml_hatelist",$query);
    }

    //删除屏蔽(只删除一条)
    public function remove_hate($user_id, $hate_id)
    {
        $query = array('user_id'=>$user_id, 'hate_id'=>$hate_id);
        if($query)  // 不能为空
        {
            $result = $this->db->where($query)->delete("ml_hatelist");     //只删除一条
            return $result;
        }
    }

    //删除屏蔽(删除多条)
    public function remove_hate_batch($query)
    {
        if($query)  // 不能为空
        {
            $result = $this->db->where($query)->delete("ml_hatelist");     //只删除一条
            return $result;
        }
    }

    //获取屏蔽数
    public function get_hate_count($user_id,$hate_id=0,$hate_addtime=0)
    {
        if($hate_id != $user_id && $hate_id == 29185)
        {
            return 1;
        }
        $this->db->select()->from("ml_hatelist")->where("user_id",$user_id);
        if($hate_id > 0)
        {
            $this->db->where("hate_id",$hate_id);
        }
        $result =  $this->db->count_all_results();
        return $result;
    }

    //获取用户屏蔽列表
    public function get_hate_id($user_id,$limit,$offset)
    {
        //
        return $hate_id_array = $this->db->where('user_id',$user_id)->order_by("addtime","desc")->get("ml_hatelist",$limit, $offset)->result_array();

    }

    /**
     * 获取用户屏蔽列表和屏蔽帖子
     * @param $user_id
     * @param string $type all:所有   post:帖子ID  user:用户ID
     * @return array|mixed
     */
    public function get_hatelist($user_id, $type = 'all')
    {
        $key = HATE_LIST_PREFIX.$user_id;
        $hate_list = unserialize($this->redis->get($key));
        if (!$hate_list) {
            $post_ids = $user_ids = array();
            $hate_res = $this->db->select("post_id")->where("user_id", $user_id)->get("ml_hatepost")->result_array();
            if ($hate_res) $post_ids = array_column($hate_res, 'post_id');

            $hate_user = $this->db->where('user_id', $user_id)->get("ml_hatelist")->result_array();
            if ($hate_user) $user_ids = array_column($hate_user, 'hate_id');

            if ($post_ids || $user_ids) {
                $hate_list = array('hate_ids' => $post_ids, 'hate_user' => $user_ids);
                $this->redis->save($key, serialize($hate_list), HATE_LIST_KEY_EXPIRE_TIME);
            }
        }

        if (!$hate_list) return array();

        if ($type == 'post') {
            $response = $hate_list['hate_ids'];
        } else if ($type == 'user') {
            $response = $hate_list['hate_user'];
        } else {
            $response = $hate_list;
        }

        return $response;
    }

}